<?php
namespace api\modules\h5\controllers\goods;

use backend\widgets\provinces\Provinces;
use common\models\goods\Goods;
use yii\data\ActiveDataProvider;
use Yii;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;
use common\models\store\Store;

/**
 * 商品接口
 *
 * Class InfoController
 * @package api\modules\h5\controllers\goods
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\goods\GoodsCommon';

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = ['view','index'];


    /**
     * 商品列表
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        $get = Yii::$app->request->get();
        $store_id = $get['store_id'];
        $keyword = $get['keyword']??'';
        $gc_id = $get['gc_id']??0;
        $status = $get['status']??null;

        $query = $this->modelClass::find()
            ->select(['goods_commonid','goods_name','goods_code','goods_bar','goods_image','goods_unit','goods_price','goods_marketprice','goods_costprice','goods_discount','goods_salenum'])
            ->where(['status'=>StatusEnum::ENABLED,'store_id'=>$store_id])
            ->orderBy('goods_commonid desc')
            ->asArray();

        if(!empty($keyword))
            $query->andWhere(['like','goods_name',$keyword]);

        if(0 < $gc_id)
            $query->andWhere(['gc_id'=>$gc_id]);

        if(!is_null($status) && is_numeric($status))
            $query->andWhere(['status'=>$status]);

        return new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Yii::$app->params['user.pageSize'],
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }

    /**
     * 获取详情
     * @param $id
     * @return array|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        return $this->getGoods($id);
    }

    /**
     * 整合商品数据
     * @param $id
     * @throws NotFoundHttpException
     */
    private function getGoods($id){
        $res_model = $this->modelClass::find()
            ->where(['goods_commonid' => $id])
            ->select(['goods_commonid','goods_name', 'goods_code', 'goods_bar', 'gc_id','brand_id', 'goods_unit','goods_price','goods_marketprice','goods_serial','status','goods_salenum'])
            ->with(['attr','goods_list','image_list','gclass','brand'])
            ->asArray()
            ->one();

        if (!$res_model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }
        foreach ($res_model['goods_list'] as &$goods){
            $_attr = [];
            foreach ($goods['attr_index'] as $attr_item){
                $_attr[] = [
                    'attr_id'=>$attr_item['attr_id'],
                    'attr_name'=>$attr_item['attr']['attr_name'],
                    'attr_value_id'=>$attr_item['attr_value_id'],
                    'attr_value_name'=>$attr_item['attr_value']['attr_value_name'],
                ];
            }
            $goods['attr'] = $_attr;
            unset($goods['attr_index']);
        }
        return $res_model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete', 'create','update']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
